package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsGoodsCommentService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.02.24
 * @since 1.0
 */
@RestController
@RequestMapping("/admin/comment")
public class DtsGoodsCommentController {

    @Autowired
    DtsGoodsCommentService goodsCommentService;

    @Autowired
    DtsBrandService brandService;

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsComment comment){
        goodsCommentService.delete(comment.getId());

        return ResponseUtil.ok();

    }

    @RequestMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false, defaultValue = "1" ) Integer page,
            @RequestParam(value = "limit",required = false, defaultValue = "10" ) Integer limit,
            Integer userId,
            @RequestParam(value = "valueId",required = false) Integer goodsId,
            String sort,String order){

        Subject subject = SecurityUtils.getSubject();

        List<Integer> brandIds = null;

        if (subject.hasRole("品牌制造商")){
          DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
             brandIds = brandService.getBrandIdsByAdminId(admin.getId());
        }

        IPage<DtsComment> ipage =  goodsCommentService.queryPage(page,limit,userId,goodsId,sort,order,brandIds);

        Map<String,Object> data = new HashMap<>();
        data.put("items",ipage.getRecords());
        data.put("total",ipage.getTotal());

        return ResponseUtil.ok(data);
    }
}
